package com.zhongli.devplatform.utils.im;

import com.alibaba.fastjson.JSONObject;
import com.zhongli.devplatform.constant.im.TencentIMConstant;
import com.zhongli.devplatform.po.im.TencentC2CMessagePO;
import org.springframework.util.Assert;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author WuXiCheng
 * @version 1.0
 */
public class TencentIMUtil {

    /**
     * 获取请求参数
     * @param request
     * @return
     */
    public static Map<String, Object> getUrlParams(HttpServletRequest request){
        Long sdkAppId = RequestUtil.getLong(request, TencentIMConstant.SDKAPPID);
        String CallbackCommand = request.getParameter(TencentIMConstant.CALLBACKCOMMAND);
        String contentType = request.getParameter(TencentIMConstant.CONTENTTYPE);
        String ClientIP = request.getParameter(TencentIMConstant.CLIENTIP);
        String OptPlatform = request.getParameter(TencentIMConstant.OPTPLATFORM);

        Map<String, Object> params = new HashMap<>();
        params.put(TencentIMConstant.SDKAPPID, sdkAppId);
        params.put(TencentIMConstant.CALLBACKCOMMAND, CallbackCommand);
        params.put(TencentIMConstant.CONTENTTYPE, contentType);
        params.put(TencentIMConstant.CLIENTIP, ClientIP);
        params.put(TencentIMConstant.OPTPLATFORM, OptPlatform);

        return params;
    }

    public static TencentC2CMessagePO parseC2CMessage(JSONObject json){
        String fromAccount = json.getString(TencentIMConstant.FROM_ACCOUNT);
        String toAccount = json.getString(TencentIMConstant.TO_ACCOUNT);
        String msgKey = json.getString(TencentIMConstant.MSGKEY);
        Integer msgSeq = json.getInteger(TencentIMConstant.MSGSEQ);
        Integer msgTime = json.getInteger(TencentIMConstant.MSGTIME);
        String msgBody = json.getJSONArray(TencentIMConstant.MSGBODY).toJSONString();

        return TencentC2CMessagePO.builder()
                .fromAccount(fromAccount)
                .toAccount(toAccount)
                .msgKey(msgKey)
                .msgSeq(msgSeq)
                .msgTime(msgTime)
                .msgBody(msgBody)
                .build();
    }

    public static HashMap<String, Object> buildReply(Boolean ActionStatus, String ErrorInfo, Integer ErrorCode){
        Assert.notNull(ActionStatus, "ActionStatus 不能为 Null");
        Assert.notNull(ErrorCode, "ErrorCode 不能为 Null");
        if(ErrorInfo == null){
            ErrorInfo = "";
        }

        HashMap<String, Object> res = new HashMap<>();
        res.put("ActionStatus", ActionStatus ? "OK" : "FAIL");  // OK / FAIL
        res.put("ErrorInfo", ErrorInfo);
        res.put("ErrorCode", ErrorCode);  // 聊天消息时  0 允许发言  1  禁止发言  其他 0 处理成功  1  处理失败
        return res;
    }

    public static HashMap<String, Object> buildOKReply(){
        return buildReply(true, "", 0);
    }

    public static HashMap<String, Object> buildFailReply(String ErrorInfo){
        return buildReply(false, ErrorInfo, 1);
    }
}
